﻿namespace NetFx.SMIL.References
{
  using System;
  using System.Text;
  using System.Collections;
  using System.Collections.Generic;

  using System.Reflection;
  using System.Reflection.Emit;
  using System.Runtime.InteropServices;

  using System.Diagnostics;
  using System.Diagnostics.CodeAnalysis;
  using System.Diagnostics.SymbolStore;
  using System.Security;
  using System.Security.Permissions;

  using NetFx.MSIL;

  /// <summary>
  /// 对参数的引用。
  /// </summary>
  public class ParameterRef:ReflectRef
  {
    #region Statics
    public new static ParameterRef it()
    {
      return new ParameterRef();
    }
    public static ParameterRef of(string identifier)
    {
      return new ParameterRef(identifier);
    }

    public static ParameterRef of(ParameterInfo parameterInfo)
    {
      return of(parameterInfo, true);
    }
    public static ParameterRef of(ParameterInfo parameterInfo,bool doBinding)
    {
      return new ParameterRef(parameterInfo, doBinding);
    }
    #endregion
    #region Fields
    /// <summary>
    /// 相关的成员。
    /// </summary>
    protected MemberRef memberRef = null;
    #endregion
    #region Properties
    /// <summary>
    /// 获取引用类型（为参数）。
    /// </summary>
    public override DefType ReflectionType
    {
      get { return DefType.Property; }
    }
    /// <summary>
    /// 获取反射信息。
    /// </summary>
    public virtual ParameterInfo ReflectedInfo
    {
      get
      {
        return this.referenced as ParameterInfo;
      }
    }
    public override bool HasReflectInfo
    {
      get
      {
        return this.ReflectedInfo != null;
      }
    }
    public override string Identifier
    {
      get
      {
        ParameterInfo info = this.ReflectedInfo;

        return info != null ? info.Name : base.Identifier;
      }
    }
    /// <summary>
    /// 获取相关的成员。
    /// </summary>
    public virtual MemberRef Member
    {
      get
      {
        return this.memberRef;
      }
    }
    #endregion
    #region Constructors
    public ParameterRef() : base(null) { }
    public ParameterRef(string identifier) : base(identifier) { }
    public ParameterRef(ParameterInfo parameterInfo,bool doBinding) 
      : base(String.Empty) 
    {
      if (parameterInfo != null)
      {
        this.identifier = parameterInfo.Name;
        if (doBinding)
        {
          this.Bind(parameterInfo);
        }
      }
    }
    #endregion
    #region Methods
    protected override void Dispose(bool disposing)
    {
      if (disposing)
      {
        this.DisposeMemberRef();
      }
      base.Dispose(disposing);
    }
    protected override ReflectRef OnBinding(object source)
    {
      ParameterInfo parameterInfo = source as ParameterInfo;
      if (parameterInfo != null)
      {
        this.DisposeMemberRef();
        this.memberRef = MemberRef.of(parameterInfo.Member);
      }
      return base.OnBinding(parameterInfo);
    }
    protected override ReflectRef OnBreaking(object referenced)
    {
      if (this.Member != null)
      {
        this.Member.Break();
      }
      return base.OnBreaking(referenced);
    }
    protected virtual void DisposeMemberRef()
    {
      if (this.memberRef != null && !this.memberRef.IsDisposed)
      {
        this.memberRef.Dispose();
      }
      this.memberRef = null;
    }
    public override string ToString()
    {
      return this.Identifier;
    }
    #endregion
  }
}
